package hn.cch.grammar.java;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayDemo {


    /**
     * 深度拷贝
     */
    @Test
    public void deepCopy() {
        String[] stringArray0 = {"0", "1"};
        System.out.println(String.join(",", stringArray0));

        // 静态方法
        String[] stringArray1 = Arrays.copyOf(stringArray0, stringArray0.length);
        System.out.println(String.join(",", stringArray1));
        // Arrays.copyOf源码调用System.arraycopy
        // System.arraycopy(stringArray0, 0, stringArray1, 0, stringArray0.length);
        System.out.println(String.join(",", stringArray1));

        // 对象克隆
        String[] stringArray2 = stringArray0.clone();
        System.out.println(String.join(",", stringArray2));


    }

    @Test
    public void test01() {

        String[] stringArray0 = {"0", "1"};
        System.out.println(String.join(",", stringArray0));

        // Arrays.asList返回静态类ArrayList，没有实现add和remove函数
        List<String> stringList0 = new ArrayList<>(Arrays.asList(stringArray0));
        System.out.println(String.join(",", stringList0));

        // Collections.addAll
        List<String> stringList1 = new ArrayList<>(stringArray0.length);
        Collections.addAll(stringList1, stringArray0);
        System.out.println(String.join(",", stringList1));

    }

    @Test
    public void test02(){
        List<String> stringList = new ArrayList<>();
        stringList.add("111");
        stringList.add("222");
        // List -> Array
        String[] stringArray = stringList.toArray(new String[0]);
        System.out.println(Arrays.toString(stringArray));
        // Array -> List
        System.out.println(new ArrayList<>(Arrays.asList(stringArray)));
    }


}
